package com.xbkj.ui_design.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.xbkj.ui_design.R;
import com.xbkj.ui_design.databinding.DropdownPhoneBrandBinding;
import com.xbkj.ui_design.databinding.FragmentDiscoveryBinding;
import com.xbkj.ui_design.databinding.FragmentHomeBinding;
import com.xbkj.ui_design.dropdown.DropDownGridMenuAdapter;
import com.xbkj.ui_design.dropdown.DropDownListMenuAdapter;
import com.xbkj.ui_design.dropdown.DropMenuItem;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @Author: XDY.
 * @Email: 2280944114@qq.com
 * @CreateTime: 2025-02-28  23:50
 * @Description: TODO
 * @Version: 1.0
 */
public class DiscoveryFragment extends Fragment {

    private FragmentDiscoveryBinding binding;

    private List<View> dropdownMenuContentViews = new ArrayList<>();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Toast.makeText( DiscoveryFragment.this.getActivity(), "新创建", Toast.LENGTH_SHORT ).show();
        binding = FragmentDiscoveryBinding.inflate(getLayoutInflater());
        initViews();
    }

    private void initViews() {
        //添加下拉菜单
        addListPopupMenuView(Arrays.asList(
                new DropMenuItem(R.string.comprehensive, 1),
                new DropMenuItem(R.string.price_desc, 2),
                new DropMenuItem(R.string.price_asc, 3),
                new DropMenuItem(R.string.id_asc, 4)
        ));
        addGridPopupMenuView(Arrays.asList(getResources().getStringArray(R.array.phone_brand).clone()));
        //添加下拉菜单
        addListPopupMenuView(Arrays.asList(
                new DropMenuItem(R.string.comprehensive, 1),
                new DropMenuItem(R.string.price_desc, 2),
                new DropMenuItem(R.string.price_asc, 3),
                new DropMenuItem(R.string.id_asc, 4)
        ));
        addGridPopupMenuView(Arrays.asList(getResources().getStringArray(R.array.phone_brand).clone()));
        binding.dropdown.setupDropDownMenu(
                Arrays.asList(
                        getString(R.string.location),
                        getString(R.string.brand),
                        getString(R.string.smart_sort),
                        getString(R.string.filter)
                ),
                dropdownMenuContentViews
        );
    }

    private void addGridPopupMenuView(List<String> menuData) {
        DropdownPhoneBrandBinding phoneBandBinding = DropdownPhoneBrandBinding.inflate(getLayoutInflater(), null, false);
        phoneBandBinding.list.setHasFixedSize(true);
        //布局管理器
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this.requireActivity(), 2);
        phoneBandBinding.list.setLayoutManager(layoutManager);
        DropDownGridMenuAdapter menuAdapter = new DropDownGridMenuAdapter(R.layout.item_dropdown);
        menuAdapter.setOnItemClickListener((adapter, view, position) -> {
            ((DropDownGridMenuAdapter) adapter).setSelect(position);
        });
        phoneBandBinding.list.setAdapter(menuAdapter);
        //重置按钮点击
        phoneBandBinding.reset.setOnClickListener(v -> {
            binding.dropdown.closeMenu();
            menuAdapter.resetSelect();
            //设置当前tab标题
            binding.dropdown.setTabText(R.string.brand, 1);
        });
        //确定点击
        phoneBandBinding.confirm.setOnClickListener(v -> {
            binding.dropdown.closeMenu();
            List<String> selects = menuAdapter.getSelect();
            String title;
            if (selects.size() > 1) {
                title = String.format("%s...", selects.get(0));
            } else if (selects.size() > 0) {
                title = selects.get(0);
            } else {
                title = getString(R.string.brand);
            }
            //设置当前tab标题
            binding.dropdown.setTabText(title, 1);
        });
        menuAdapter.setNewInstance(menuData);
        dropdownMenuContentViews.add(phoneBandBinding.getRoot());
    }


    /**
     * 添加单选列表下拉菜单
     *
     * @param menuData
     */
    private void addListPopupMenuView(List<DropMenuItem> menuData) {
        RecyclerView recyclerView = new RecyclerView(this.requireActivity());
        recyclerView.setBackgroundResource(R.drawable.shape_surface);
        recyclerView.setLayoutManager(new LinearLayoutManager(this.requireActivity()));
        DropDownListMenuAdapter menuAdapter = new DropDownListMenuAdapter(R.layout.item_dropdown);
        menuAdapter.setOnItemClickListener((adapter, view, position) -> {
            //关闭下拉菜单
            binding.dropdown.closeMenu();
            DropMenuItem menuItem = (DropMenuItem) adapter.getItem(position);
            //设置当前tab标题
            binding.dropdown.setTabText(menuItem.getTitle(), 0);
            //设置当前位置选中
            ((DropDownListMenuAdapter) adapter).setSelect(position);
        });
        recyclerView.setAdapter(menuAdapter);
        dropdownMenuContentViews.add(recyclerView);
        menuAdapter.setNewInstance(menuData);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return binding.getRoot();
    }

    public static DiscoveryFragment newInstance() {
        Bundle args = new Bundle();
        DiscoveryFragment fragment = new DiscoveryFragment();
        fragment.setArguments(args);
        return fragment;
    }
}